package com.centerm.bluetooth.lklcommon.controller;

import android.content.Context;
import android.support.annotation.NonNull;

import com.centerm.bluetooth.core.controller.BaseController;
import com.centerm.bluetooth.core.controller.constants.WaitEnum;
import com.centerm.bluetooth.ibridge.BluetoothIBridgeDevice;
import com.centerm.bluetooth.lklcommon.Command;
import com.centerm.bluetooth.lklcommon.listener.IPinListener;
import com.centerm.bluetooth.model.Respond;
import com.centerm.util.HexUtil;

import java.io.IOException;

/**
 * Created by linwenhui on 2017/5/12.
 */

public class PinController extends BaseController<IPinListener> {

    public PinController(@NonNull Context context) {
        super(context);
    }

    public boolean startInputPin(int pinLen, int timeout) throws IOException {

        if (wait != WaitEnum.FREE)
            return false;

        byte[] params = new byte[]{(byte) pinLen, (byte) timeout};
        boolean res = send(Command.INPUT_PIN, params);
        if (res) {
            setTimeOut(timeout);
            if (baseListener != null)
                baseListener.onInputWaiter();
        }
        return res;
    }

    public boolean startInputPinSafe(int pinLen, int timeout, int safeFlag) throws IOException {
        if (wait != WaitEnum.FREE)
            return false;

        byte[] params = new byte[]{(byte) pinLen, (byte) timeout, (byte) safeFlag};
        boolean res = send(Command.INPUT_PIN_SAFE, params);
        if (res) {
            setTimeOut(timeout);
            if (baseListener != null)
                baseListener.onInputWaiter();
        }
        return res;
    }

    @Override
    public void onDataReceived(BluetoothIBridgeDevice device, Respond respond) {
        super.onDataReceived(device, respond);
        String cmdString = HexUtil.byte2HexStr(respond.getCmd()).toUpperCase();
        if (Command.INPUT_PIN_STRING.equals(cmdString)) {
            parseInputPinResult(respond.getContent());
        } else if (Command.INPUT_PIN_SAFE_STRING.equals(cmdString)) {
            parseInputPinSafeResult(respond.getContent());
        }
    }

    protected void parseInputPinResult(byte[] datas) {
        if (datas[0] == 0x00) {
            // 随机数
            byte[] random = new byte[8];
            System.arraycopy(datas, 1, random, 0, 8);

            // 加密后的密码长度
            byte pinblockLen = datas[9];

            // 加密后的 加密后的 PINPIN BLOCK
            if (pinblockLen > 0) {
                byte[] pinblock = new byte[8];
                System.arraycopy(datas, 10, pinblock, 0, pinblock.length);
                if (baseListener != null) {
                    baseListener.onInputPin(HexUtil.byte2HexStr(random), HexUtil.byte2HexStr(pinblock), null, null);
                }
            } else {
                if (baseListener != null)
                    baseListener.onInputPin(HexUtil.byte2HexStr(random), null, null, null);
            }
        } else {
            if (baseListener != null)
                baseListener.onInputPinCancel();
        }
    }

    protected void parseInputPinSafeResult(byte[] datas) {
        if (datas[0] == 0x00 || datas[0] == 0xFF) {
            // 随机数
            byte[] random = new byte[8];
            System.arraycopy(datas, 1, random, 0, 8);

            // 加密后的密码长度
            byte pinblockLen = datas[9];

            // 加密后的 加密后的 PINPIN BLOCK
            byte[] pinblock = null;
            if (pinblockLen > 0 && pinblockLen != 0xFF) {
                pinblock = new byte[pinblockLen];
                System.arraycopy(datas, 10, pinblock, 0, pinblockLen);
            }

            // MAC 随机数
            byte[] macRandom = new byte[8];
            System.arraycopy(datas, 18, macRandom, 0, 8);

            // MAC 值
            byte[] macLenBytes = new byte[2];
            System.arraycopy(datas, 26, macLenBytes, 0, 2);
            int macLen = 0;
            try {
                macLen = Integer.valueOf(HexUtil.byte2HexStr(macLenBytes));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            byte[] mac = null;
            if (macLen > 0) {
                mac = new byte[macLen];
                System.arraycopy(datas, 28, mac, 0, macLen);
            }

            if (baseListener != null) {
                baseListener.onInputPin(HexUtil.byte2HexStr(random), HexUtil.byte2HexStr(pinblock), HexUtil.byte2HexStr(macRandom), HexUtil.byte2HexStr(mac));
            }

        } else {
            if (baseListener != null)
                baseListener.onInputPinCancel();
        }
    }
}
